local Players = game:GetService("Players")

local CommonOHOH = _G.CommonOHOH;
local ConnectClass=_G.CommonOHOH.load.Connect();
local M = CommonOHOH.class.new(script, CommonOHOH.files.HomeCar());

local CarTool=_G.CommonOHOH.load.CarTool();
local CallBackManager = CommonOHOH.load.CallBackManager();

local InstanceManager = CommonOHOH.load.InstanceManager();

-- 初始化一辆车
function M.new(...)
	return M(...);
end;

function M.ctor(self,player,bodyId,flagId,ringId,tailId,weaponId)--构造函数

    self.DataStore = player:GetComponent(_G.ServerOHOH.load.Server_PlayerDataComponent());

    local bodyId,flagId,ringId,tailId,weaponId
    for carId,v in pairs( self.DataStore:GetData("car").skin.__real) do
        local ctype=CarTool.GetPartCfgData(carId,true).type
        if ctype==1 then --钻头
            weaponId=carId
        elseif ctype==2 then-- 车轮
            ringId=carId
        elseif ctype==3 then-- 车身
            bodyId=carId
        elseif ctype==4 then-- 推进器
            tailId=carId
        elseif ctype==5 then-- 装饰
            flagId=carId
        end
    end
   
	M.super.ctor(self,player,bodyId,flagId,ringId,tailId,weaponId);
    
    
    self:AddForceField(self:GetModel())
    self.GetRewardEvent=ConnectClass.new() --离开车辆事件

    self.playerList={}

    CallBackManager:AddRbxConnect(self,game.Players.PlayerRemoving, function(obj,player) --车上的玩家退出游戏 处理
        self.playerList[player.UserId]=nil
	end);

    
    
end;



-- 获取车上的所有玩家
function M.GetAllPlayer(self)
    local list={}
    for userId,player in pairs(self.playerList) do
        table.insert(list,player)
    end
    return list
end


function M.AddCharacter(self,main_inst)
    M.super.AddCharacter(self,main_inst);
   local character InstanceManager:FindObject(main_inst)
   if character and character.player then
        self.playerList[character.player:GetUserId()]=character.player
   end
end
---参数 forceField的父物体
function M.AddForceField(self,part)
    local forceField = Instance.new("ForceField")
    forceField.Visible = true 
    forceField.Parent = part
end

---参数 forceField的父物体
function M.RemoveForceField(self,part)
    for _, child in ipairs(part:GetChildren()) do
        if child:IsA("ForceField") then
            child:Destroy()
            -- break -- 找到后可以退出循环
        end
    end
end


function M.dtor(self)
	M.super.dtor(self);
end;


function M.Boom(self)
    print("Boom")
    if  not self.battleEnd then
        self.battleEnd = true
    end
    self:SetCarHumanoidSpeed(0)
    self:SetDriveConnectionOff()
    task.wait()
    self:SetEmitterStatus(true)
    self:SetDrillingSpeed(800)
    self:RemoveForceField(self:GetModel())
    task.wait(3)
    
    -- task.wait()
    
    local obj=self.main_inst
    self:CustomLeaveCar()
    self:CarCrash()
    self:KillCharacter()
   
    game:GetService("Debris"):AddItem(obj,10)
    self.player.EnterBattleEvent:Action(false)
    self:Wait(2)
    self:dtor()
    -- self:AddProcess(function()
    --     self:Wait(8)
    --     self.player.EnterBattleEvent:Action(false)
    -- end)

end




function M.Start(self,folder)
    print("_______________Start_________________________________")
    self:AddProcess(function()
        -- self:AddCharacter(self.player.character.main_inst) --将玩家放置到车上
        -- self:Wait(1)
        self:SetCarHumanoidSpeed(0)--禁止玩家操纵
        self:AnchoredSwitch(false) --取消锚固
        
        self:Wait(0.5)
        self:AddPrivateComponentByFolder(script.startCom) --添加控制器
        self:CustomAutoDrive(folder) --自动驾驶
    end)
end



return M